package com.nanbei.eduService.controller;


import com.nanbei.commonUntil.R;
import com.nanbei.eduService.entity.EduChapter;
import com.nanbei.eduService.entity.vo.ChapterVo;
import com.nanbei.eduService.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程ID查询章节和小结
     * @param courseId
     * @return
     */
    @GetMapping("findChapterVideoById/{courseId}")
    public R findChapterVideoById(@PathVariable String courseId){
        List<ChapterVo> chapterVoList = eduChapterService.findChapterVideoById(courseId);
        return R.ok().data("chapterVoList",chapterVoList);
    }

    /**
     * 添加章节
     * @param eduChapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
         eduChapterService.save(eduChapter);
         return R.ok();
    }

    /**
     * 根据id查询章节信息
     * @param chapterId
     * @return
     */
    @GetMapping("findChapterById/{chapterId}")
    public R findChapterById(@PathVariable String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("eduChapterInfo" ,eduChapter);
    }

    /**
     * 修改章节信息
     * @param eduChapter
     * @return
     */
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }


    /**
     * 根据ID删除章节信息，章节中如果含有小节则不能删除
     * @param chapterId
     * @return
     */
    @DeleteMapping("delChapterById/{chapterId}")
    public R delChapterById(@PathVariable String chapterId){
        Boolean a = eduChapterService.delChapterById(chapterId);
        if (a){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

